package com.tuling.code.transaction.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvocationHandler implements InvocationHandler {
	
	private Object target;  //目标对象
	
	public MyInvocationHandler(Object target){
		this.target = target;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if(method.getName().startsWith("test")){
			System.out.println("*************加入了增强日志处理***********");
		}
		return method.invoke(target, args);
	}

	public static void main(String[] args) {
		PersonService ps = new PersonServiceImpl();
		MyInvocationHandler handler = new MyInvocationHandler(ps);
		PersonService proxy = (PersonService) Proxy.newProxyInstance(
				MyInvocationHandler.class.getClassLoader(), 
				new Class[]{PersonService.class}, handler);
		proxy.test1();
	}
}
